﻿/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    public class AnimationInspector : EventBaseInspector
    {
        private Label animationClipLenth;
        private Label animationLoop;

        private Toggle rootMotionToggle;

        private AnimationEvent animationData;

        private FloatField transitionTimeField;


        public override void Show(VisualElement root)
        {
            this.root = root;
            itemStyle = ActionEditorInspector.currentTrackItemStyle;
            data = itemStyle.eventData;
            animationData = data as AnimationEvent;

            ShowTrackName();
            ShowAnimationClip();
            ShowStartIndex();
            ShowDurationIndex();
            animationClipLenth = new Label("动画资源长度");
            base.root.Add(animationClipLenth);
            animationLoop = new Label("循环动画");
            base.root.Add(animationLoop);
            ShowAnimationInfo();
            ShowRootMotion();
            ShowTransitionTime();

        }

        private void ShowAnimationClip()
        {
            ObjectField animationClipAssetField = new ObjectField("动画资源");
            animationClipAssetField.objectType = typeof(AnimationClip);
            animationClipAssetField.value = animationData.animationClip;
            animationClipAssetField.RegisterValueChangedCallback(p =>
            {
                if (p.newValue == null) return;
                AnimationClip clip = p.newValue as AnimationClip;
                // 修改自身显示效果
                animationClipLenth.text = "动画资源长度:      " + ((int)(clip.length * clip.frameRate));
                animationLoop.text = "循环动画:     " + clip.isLooping;
                // 保存到配置
                animationData.animationClip = clip;
                animationData.durationIndex = (int)(clip.length * clip.frameRate);
                itemStyle.ResetView();
            });
            root.Add(animationClipAssetField);
        }

        private void ShowAnimationInfo()
        {
            if (animationData.animationClip != null)
            {
                AnimationClip clip = animationData.animationClip;
                animationClipLenth.text = "动画资源长度:      " + ((int)(clip.length * clip.frameRate));
                animationLoop.text = "循环动画:     " + clip.isLooping;
            }
        }

        private void ShowRootMotion()
        {
            rootMotionToggle = new Toggle("应用根运动");
            rootMotionToggle.value = animationData.applyRootMotion;
            rootMotionToggle.RegisterValueChangedCallback(p => { animationData.applyRootMotion = p.newValue; });
            root.Add(rootMotionToggle);

        }

        private void ShowTransitionTime()
        {
            transitionTimeField = new FloatField("过渡时间");
            transitionTimeField.value = animationData.TransitionTime;
            transitionTimeField.RegisterCallback<FocusOutEvent>(p =>
            {
                if (transitionTimeField.value != animationData.TransitionTime)
                {
                    animationData.TransitionTime = transitionTimeField.value;
                }
            });
            root.Add(transitionTimeField);
        }
    }
}
